package com.cxd.base.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.service.ManageService;

/**
 * @author wtj
 * @date 2015年12月1日 下午4:16:20
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
@Controller("ManageController")
@RequestMapping("/managec")
public class ManageController extends BaseController{
	private static final Logger logger = Logger.getLogger(BaseController.class);
	
	@Resource
	ManageService manageService;
	
	/**
	 * @方法说明:批量删除数据
	 * @author 王统江 
	 * @return void
	 * @date 2013-12-22 下午11:49:01
	 */
	@RequestMapping(value = "/delListData", method = {RequestMethod.GET,RequestMethod.POST})
	public void delListData(HttpServletRequest request,HttpServletResponse response,String delDateStr)throws IOException{
		manageService.delListData(delDateStr);
	}
	
	/**
	 * @方法说明:更改数据发布状态
	 * @author 王统江 
	 * @return void
	 * @date 2014-1-2 下午3:46:45
	 */
	@RequestMapping(value = "/macroUpdateStatus", method = { RequestMethod.GET,RequestMethod.POST })
	public void macroUpdateStatus(HttpServletRequest request,
			HttpServletResponse response, String tableName,String id,String status)
			throws IOException {
		JSONObject json = new JSONObject();
		try {
			manageService.updateStatus(tableName,id,status);
			this.jsonMsg(json, 200, "修改成功!");
		} catch(CxdException e){
			super.cxdJsonException(json, e,"【tableName】"+tableName+"【id】"+id+"【status】"+status);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	@RequestMapping(value = "/updateById", method = { RequestMethod.GET,RequestMethod.POST })
	public void updateById(HttpServletRequest request,
			HttpServletResponse response, String tableName,String id,String field,String value)
			throws IOException {
		JSONObject json = new JSONObject();
		try {
			manageService.updateById(tableName,id,field,value);
			this.jsonMsg(json, 200, "修改成功!");
		} catch(CxdException e){
			super.cxdJsonException(json, e,"无参数");
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/macroDeleteStatus", method = RequestMethod.POST)	
	public void delInfo(HttpServletRequest request,HttpServletResponse response,String tableName ,String field,String value)	throws IOException {
		JSONObject json = new JSONObject();
		try {
			db.delLog(tableName, new String[]{field}, new String[]{value});
			json.put("success", true);
			json.put("msg", "删除成功！");
		} catch (CxdException e) {
			super.cxdJsonException(json, e,"无参数");
		}
		super.responseSendMsg(response, json);
	}
}